home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / class.ezpdf.php < prev    next >
PHP Script  |  2003-03-12  |  56KB  |  1,555 lines

  1. <?php
  2.  
  3. include_once('class.pdf.php');
  4.  
  5. class Cezpdf extends Cpdf {
  6. //==============================================================================
  7. // this class will take the basic interaction facilities of the Cpdf class
  8. // and make more useful functions so that the user does not have to 
  9. // know all the ins and outs of pdf presentation to produce something pretty.
  10. //
  11. // IMPORTANT NOTE
  12. // there is no warranty, implied or otherwise with this software.
  13. // 
  14. // version 009 (versioning is linked to class.pdf.php)
  15. //
  16. // released under a public domain licence.
  17. //
  18. // Wayne Munro, R&OS Ltd, http://www.ros.co.nz/pdf
  19. //==============================================================================
  20.  
  21. var $ez=array('fontSize'=>10); // used for storing most of the page configuration parameters
  22. var $y; // this is the current vertical positon on the page of the writing point, very important
  23. var $ezPages=array(); // keep an array of the ids of the pages, making it easy to go back and add page numbers etc.
  24. var $ezPageCount=0;
  25.  
  26. // ------------------------------------------------------------------------------
  27.  
  28. function Cezpdf($paper='a4',$orientation='portrait'){
  29.     // Assuming that people don't want to specify the paper size using the absolute coordinates
  30.     // allow a couple of options:
  31.     // orientation can be 'portrait' or 'landscape'
  32.     // or, to actually set the coordinates, then pass an array in as the first parameter.
  33.     // the defaults are as shown.
  34.     // 
  35.     // -------------------------
  36.     // 2002-07-24 - Nicola Asuni (info@tecnick.com):
  37.     // Added new page formats (45 standard ISO paper formats and 4 american common formats)
  38.     // paper cordinates are calculated in this way: (inches * 72) where 1 inch = 2.54 cm
  39.     // 
  40.     // Now you may also pass a 2 values array containing the page width and height in centimeters
  41.     // -------------------------
  42.  
  43.     if (!is_array($paper)){
  44.         switch (strtoupper($paper)){
  45.             case '4A0': {$size = array(0,0,4767.87,6740.79); break;}
  46.             case '2A0': {$size = array(0,0,3370.39,4767.87); break;}
  47.             case 'A0': {$size = array(0,0,2383.94,3370.39); break;}
  48.             case 'A1': {$size = array(0,0,1683.78,2383.94); break;}
  49.             case 'A2': {$size = array(0,0,1190.55,1683.78); break;}
  50.             case 'A3': {$size = array(0,0,841.89,1190.55); break;}
  51.             case 'A4': default: {$size = array(0,0,595.28,841.89); break;}
  52.             case 'A5': {$size = array(0,0,419.53,595.28); break;}
  53.             case 'A6': {$size = array(0,0,297.64,419.53); break;}
  54.             case 'A7': {$size = array(0,0,209.76,297.64); break;}
  55.             case 'A8': {$size = array(0,0,147.40,209.76); break;}
  56.             case 'A9': {$size = array(0,0,104.88,147.40); break;}
  57.             case 'A10': {$size = array(0,0,73.70,104.88); break;}
  58.             case 'B0': {$size = array(0,0,2834.65,4008.19); break;}
  59.             case 'B1': {$size = array(0,0,2004.09,2834.65); break;}
  60.             case 'B2': {$size = array(0,0,1417.32,2004.09); break;}
  61.             case 'B3': {$size = array(0,0,1000.63,1417.32); break;}
  62.             case 'B4': {$size = array(0,0,708.66,1000.63); break;}
  63.             case 'B5': {$size = array(0,0,498.90,708.66); break;}
  64.             case 'B6': {$size = array(0,0,354.33,498.90); break;}
  65.             case 'B7': {$size = array(0,0,249.45,354.33); break;}
  66.             case 'B8': {$size = array(0,0,175.75,249.45); break;}
  67.             case 'B9': {$size = array(0,0,124.72,175.75); break;}
  68.             case 'B10': {$size = array(0,0,87.87,124.72); break;}
  69.             case 'C0': {$size = array(0,0,2599.37,3676.54); break;}
  70.             case 'C1': {$size = array(0,0,1836.85,2599.37); break;}
  71.             case 'C2': {$size = array(0,0,1298.27,1836.85); break;}
  72.             case 'C3': {$size = array(0,0,918.43,1298.27); break;}
  73.             case 'C4': {$size = array(0,0,649.13,918.43); break;}
  74.             case 'C5': {$size = array(0,0,459.21,649.13); break;}
  75.             case 'C6': {$size = array(0,0,323.15,459.21); break;}
  76.             case 'C7': {$size = array(0,0,229.61,323.15); break;}
  77.             case 'C8': {$size = array(0,0,161.57,229.61); break;}
  78.             case 'C9': {$size = array(0,0,113.39,161.57); break;}
  79.             case 'C10': {$size = array(0,0,79.37,113.39); break;}
  80.             case 'RA0': {$size = array(0,0,2437.80,3458.27); break;}
  81.             case 'RA1': {$size = array(0,0,1729.13,2437.80); break;}
  82.             case 'RA2': {$size = array(0,0,1218.90,1729.13); break;}
  83.             case 'RA3': {$size = array(0,0,864.57,1218.90); break;}
  84.             case 'RA4': {$size = array(0,0,609.45,864.57); break;}
  85.             case 'SRA0': {$size = array(0,0,2551.18,3628.35); break;}
  86.             case 'SRA1': {$size = array(0,0,1814.17,2551.18); break;}
  87.             case 'SRA2': {$size = array(0,0,1275.59,1814.17); break;}
  88.             case 'SRA3': {$size = array(0,0,907.09,1275.59); break;}
  89.             case 'SRA4': {$size = array(0,0,637.80,907.09); break;}
  90.             case 'LETTER': {$size = array(0,0,612.00,792.00); break;}
  91.             case 'LEGAL': {$size = array(0,0,612.00,1008.00); break;}
  92.             case 'EXECUTIVE': {$size = array(0,0,521.86,756.00); break;}
  93.             case 'FOLIO': {$size = array(0,0,612.00,936.00); break;}
  94.         }
  95.         switch (strtolower($orientation)){
  96.             case 'landscape':
  97.                 $a=$size[3];
  98.                 $size[3]=$size[2];
  99.                 $size[2]=$a;
  100.                 break;
  101.         }
  102.     } else {
  103.         if (count($paper)>2) {
  104.             // then an array was sent it to set the size
  105.             $size = $paper;
  106.         }
  107.         else { //size in centimeters has been passed
  108.             $size[0] = 0;
  109.             $size[1] = 0;
  110.             $size[2] = ( $paper[0] / 2.54 ) * 72;
  111.             $size[3] = ( $paper[1] / 2.54 ) * 72;
  112.         }
  113.     }
  114.     $this->Cpdf($size);
  115.     $this->ez['pageWidth']=$size[2];
  116.     $this->ez['pageHeight']=$size[3];
  117.     
  118.     // also set the margins to some reasonable defaults
  119.     $this->ez['topMargin']=30;
  120.     $this->ez['bottomMargin']=30;
  121.     $this->ez['leftMargin']=30;
  122.     $this->ez['rightMargin']=30;
  123.     
  124.     // set the current writing position to the top of the first page
  125.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  126.     // and get the ID of the page that was created during the instancing process.
  127.     $this->ezPages[1]=$this->getFirstPageId();
  128.     $this->ezPageCount=1;
  129. }
  130.  
  131. // ------------------------------------------------------------------------------
  132. // 2002-07-24: Nicola Asuni (info@tecnick.com)
  133. // Set Margins in centimeters
  134. function ezSetCmMargins($top,$bottom,$left,$right){
  135.     $top = ( $top / 2.54 ) * 72;
  136.     $bottom = ( $bottom / 2.54 ) * 72;
  137.     $left = ( $left / 2.54 ) * 72;
  138.     $right = ( $right / 2.54 ) * 72;
  139.     $this->ezSetMargins($top,$bottom,$left,$right);
  140. }
  141. // ------------------------------------------------------------------------------
  142.  
  143.  
  144. function ezColumnsStart($options=array()){
  145.   // start from the current y-position, make the set number of columne
  146.   if (isset($this->ez['columns']) && $this->ez['columns']==1){
  147.     // if we are already in a column mode then just return.
  148.     return;
  149.   }
  150.   $def=array('gap'=>10,'num'=>2);
  151.   foreach($def as $k=>$v){
  152.     if (!isset($options[$k])){
  153.       $options[$k]=$v;
  154.     }
  155.   }
  156.   // setup the columns
  157.   $this->ez['columns']=array('on'=>1,'colNum'=>1);
  158.  
  159.   // store the current margins
  160.   $this->ez['columns']['margins']=array(
  161.      $this->ez['leftMargin']
  162.     ,$this->ez['rightMargin']
  163.     ,$this->ez['topMargin']
  164.     ,$this->ez['bottomMargin']
  165.   );
  166.   // and store the settings for the columns
  167.   $this->ez['columns']['options']=$options;
  168.   // then reset the margins to suit the new columns
  169.   // safe enough to assume the first column here, but start from the current y-position
  170.   $this->ez['topMargin']=$this->ez['pageHeight']-$this->y;
  171.   $width=($this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin']-($options['num']-1)*$options['gap'])/$options['num'];
  172.   $this->ez['columns']['width']=$width;
  173.   $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  174.   
  175. }
  176. // ------------------------------------------------------------------------------
  177. function ezColumnsStop(){
  178.   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  179.     $this->ez['columns']['on']=0;
  180.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0];
  181.     $this->ez['rightMargin']=$this->ez['columns']['margins'][1];
  182.     $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  183.     $this->ez['bottomMargin']=$this->ez['columns']['margins'][3];
  184.   }
  185. }
  186. // ------------------------------------------------------------------------------
  187. function ezInsertMode($status=1,$pageNum=1,$pos='before'){
  188.   // puts the document into insert mode. new pages are inserted until this is re-called with status=0
  189.   // by default pages wil be inserted at the start of the document
  190.   switch($status){
  191.     case '1':
  192.       if (isset($this->ezPages[$pageNum])){
  193.         $this->ez['insertMode']=1;
  194.         $this->ez['insertOptions']=array('id'=>$this->ezPages[$pageNum],'pos'=>$pos);
  195.       }
  196.       break;
  197.     case '0':
  198.       $this->ez['insertMode']=0;
  199.       break;
  200.   }
  201. }
  202. // ------------------------------------------------------------------------------
  203.  
  204. function ezNewPage(){
  205.   $pageRequired=1;
  206.   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  207.     // check if this is just going to a new column
  208.     // increment the column number
  209. //echo 'HERE<br>';
  210.     $this->ez['columns']['colNum']++;
  211. //echo $this->ez['columns']['colNum'].'<br>';
  212.     if ($this->ez['columns']['colNum'] <= $this->ez['columns']['options']['num']){
  213.       // then just reset to the top of the next column
  214.       $pageRequired=0;
  215.     } else {
  216.       $this->ez['columns']['colNum']=1;
  217.       $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  218.     }
  219.  
  220.     $width = $this->ez['columns']['width'];
  221.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0]+($this->ez['columns']['colNum']-1)*($this->ez['columns']['options']['gap']+$width);
  222.     $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  223.   }
  224. //echo 'left='.$this->ez['leftMargin'].'   right='.$this->ez['rightMargin'].'<br>';
  225.  
  226.   if ($pageRequired){
  227.     // make a new page, setting the writing point back to the top
  228.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  229.     // make the new page with a call to the basic class.
  230.     $this->ezPageCount++;
  231.     if (isset($this->ez['insertMode']) && $this->ez['insertMode']==1){
  232.       $id = $this->ezPages[$this->ezPageCount] = $this->newPage(1,$this->ez['insertOptions']['id'],$this->ez['insertOptions']['pos']);
  233.       // then manipulate the insert options so that inserted pages follow each other
  234.       $this->ez['insertOptions']['id']=$id;
  235.       $this->ez['insertOptions']['pos']='after';
  236.     } else {
  237.       $this->ezPages[$this->ezPageCount] = $this->newPage();
  238.     }
  239.   } else {
  240.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  241.   }
  242. }
  243.  
  244. // ------------------------------------------------------------------------------
  245.  
  246. function ezSetMargins($top,$bottom,$left,$right){
  247.   // sets the margins to new values
  248.   $this->ez['topMargin']=$top;
  249.   $this->ez['bottomMargin']=$bottom;
  250.   $this->ez['leftMargin']=$left;
  251.   $this->ez['rightMargin']=$right;
  252.   // check to see if this means that the current writing position is outside the 
  253.   // writable area
  254.   if ($this->y > $this->ez['pageHeight']-$top){
  255.     // then move y down
  256.     $this->y = $this->ez['pageHeight']-$top;
  257.   }
  258.   if ( $this->y < $bottom){
  259.     // then make a new page
  260.     $this->ezNewPage();
  261.   }
  262. }  
  263.  
  264. // ------------------------------------------------------------------------------
  265.  
  266. function ezGetCurrentPageNumber(){
  267.   // return the strict numbering (1,2,3,4..) number of the current page
  268.   return $this->ezPageCount;
  269. }
  270.  
  271. // ------------------------------------------------------------------------------
  272.  
  273. function ezStartPageNumbers($x,$y,$size,$pos='left',$pattern='{PAGENUM} of {TOTALPAGENUM}',$num=''){
  274.   // put page numbers on the pages from here.
  275.   // place then on the 'pos' side of the coordinates (x,y).
  276.   // pos can be 'left' or 'right'
  277.   // use the given 'pattern' for display, where (PAGENUM} and {TOTALPAGENUM} are replaced
  278.   // as required.
  279.   // if $num is set, then make the first page this number, the number of total pages will
  280.   // be adjusted to account for this.
  281.   // Adjust this function so that each time you 'start' page numbers then you effectively start a different batch
  282.   // return the number of the batch, so that they can be stopped in a different order if required.
  283.   if (!$pos || !strlen($pos)){
  284.     $pos='left';
  285.   }
  286.   if (!$pattern || !strlen($pattern)){
  287.     $pattern='{PAGENUM} of {TOTALPAGENUM}';
  288.   }
  289.   if (!isset($this->ez['pageNumbering'])){
  290.     $this->ez['pageNumbering']=array();
  291.   }
  292.   $i = count($this->ez['pageNumbering']);
  293.   $this->ez['pageNumbering'][$i][$this->ezPageCount]=array('x'=>$x,'y'=>$y,'pos'=>$pos,'pattern'=>$pattern,'num'=>$num,'size'=>$size);
  294.   return $i;
  295. }
  296.  
  297. // ------------------------------------------------------------------------------
  298.  
  299. function ezWhatPageNumber($pageNum,$i=0){
  300.   // given a particular generic page number (ie, document numbered sequentially from beginning),
  301.   // return the page number under a particular page numbering scheme ($i)
  302.   $num=0;
  303.   $start=1;
  304.   $startNum=1;
  305.   if (!isset($this->ez['pageNumbering']))
  306.   {
  307.     $this->addMessage('WARNING: page numbering called for and wasn\'t started with ezStartPageNumbers');
  308.     return 0;
  309.   }
  310.   foreach($this->ez['pageNumbering'][$i] as $k=>$v){
  311.     if ($k<=$pageNum){
  312.       if (is_array($v)){
  313.         // start block
  314.         if (strlen($v['num'])){
  315.           // a start was specified
  316.           $start=$v['num'];
  317.           $startNum=$k;
  318.           $num=$pageNum-$startNum+$start;
  319.         }
  320.       } else {
  321.         // stop block
  322.         $num=0;
  323.       }
  324.     }
  325.   }
  326.   return $num;
  327. }
  328.  
  329. // ------------------------------------------------------------------------------
  330.  
  331. function ezStopPageNumbers($stopTotal=0,$next=0,$i=0){
  332.   // if stopTotal=1 then the totalling of pages for this number will stop too
  333.   // if $next=1, then do this page, but not the next, else do not do this page either
  334.   // if $i is set, then stop that particular pagenumbering sequence.
  335.   if (!isset($this->ez['pageNumbering'])){
  336.     $this->ez['pageNumbering']=array();
  337.   }
  338.   if ($next && isset($this->ez['pageNumbering'][$i][$this->ezPageCount]) && is_array($this->ez['pageNumbering'][$i][$this->ezPageCount])){
  339.     // then this has only just been started, this will over-write the start, and nothing will appear
  340.     // add a special command to the start block, telling it to stop as well
  341.     if ($stopTotal){
  342.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stoptn']=1;
  343.     } else {
  344.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stopn']=1;
  345.     }
  346.   } else {
  347.     if ($stopTotal){
  348.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stopt';
  349.     } else {
  350.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stop';
  351.     }
  352.     if ($next){
  353.       $this->ez['pageNumbering'][$i][$this->ezPageCount].='n';
  354.     }
  355.   }
  356. }
  357.  
  358. // ------------------------------------------------------------------------------
  359.  
  360. function ezPRVTpageNumberSearch($lbl,&$tmp){
  361.   foreach($tmp as $i=>$v){
  362.     if (is_array($v)){
  363.       if (isset($v[$lbl])){
  364.         return $i;
  365.       }
  366.     } else {
  367.       if ($v==$lbl){
  368.         return $i;
  369.       }
  370.     }
  371.   }
  372.   return 0;
  373. }
  374.  
  375. // ------------------------------------------------------------------------------
  376.  
  377. function ezPRVTaddPageNumbers(){
  378.   // this will go through the pageNumbering array and add the page numbers are required
  379.   if (isset($this->ez['pageNumbering'])){
  380.     $totalPages1 = $this->ezPageCount;
  381.     $tmp1=$this->ez['pageNumbering'];
  382.     $status=0;
  383.     foreach($tmp1 as $i=>$tmp){
  384.       // do each of the page numbering systems
  385.       // firstly, find the total pages for this one
  386.       $k = $this->ezPRVTpageNumberSearch('stopt',$tmp);
  387.       if ($k && $k>0){
  388.         $totalPages = $k-1;
  389.       } else {
  390.         $l = $this->ezPRVTpageNumberSearch('stoptn',$tmp);
  391.         if ($l && $l>0){
  392.           $totalPages = $l;
  393.         } else {
  394.           $totalPages = $totalPages1;
  395.         }
  396.       }
  397.       foreach ($this->ezPages as $pageNum=>$id){
  398.         if (isset($tmp[$pageNum])){
  399.           if (is_array($tmp[$pageNum])){
  400.             // then this must be starting page numbers
  401.             $status=1;
  402.             $info = $tmp[$pageNum];
  403.             $info['dnum']=$info['num']-$pageNum;
  404.             // also check for the special case of the numbering stopping and starting on the same page
  405.             if (isset($info['stopn']) || isset($info['stoptn']) ){
  406.               $status=2;
  407.             }
  408.           } else if ($tmp[$pageNum]=='stop' || $tmp[$pageNum]=='stopt'){
  409.             // then we are stopping page numbers
  410.             $status=0;
  411.           } else if ($status==1 && ($tmp[$pageNum]=='stoptn' || $tmp[$pageNum]=='stopn')){
  412.             // then we are stopping page numbers
  413.             $status=2;
  414.           }
  415.         }
  416.         if ($status){
  417.           // then add the page numbering to this page
  418.           if (strlen($info['num'])){
  419.             $num=$pageNum+$info['dnum'];
  420.           } else {
  421.             $num=$pageNum;
  422.           }
  423.           $total = $totalPages+$num-$pageNum;
  424.           $pat = str_replace('{PAGENUM}',$num,$info['pattern']);
  425.           $pat = str_replace('{TOTALPAGENUM}',$total,$pat);
  426.           $this->reopenObject($id);
  427.           switch($info['pos']){
  428.             case 'right':
  429.               $this->addText($info['x'],$info['y'],$info['size'],$pat);
  430.               break;
  431.             default:
  432.               $w=$this->getTextWidth($info['size'],$pat);
  433.               $this->addText($info['x']-$w,$info['y'],$info['size'],$pat);
  434.               break;
  435.           }
  436.           $this->closeObject();
  437.         }
  438.         if ($status==2){
  439.           $status=0;
  440.         }
  441.       }
  442.     }
  443.   }
  444. }
  445.  
  446. // ------------------------------------------------------------------------------
  447.  
  448. function ezPRVTcleanUp(){
  449.   $this->ezPRVTaddPageNumbers();
  450. }
  451.  
  452. // ------------------------------------------------------------------------------
  453.  
  454. function ezStream($options=''){
  455.   $this->ezPRVTcleanUp();
  456.   $this->stream($options);
  457. }
  458.  
  459. // ------------------------------------------------------------------------------
  460.  
  461. function ezOutput($options=0){
  462.   $this->ezPRVTcleanUp();
  463.   return $this->output($options);
  464. }
  465.  
  466. // ------------------------------------------------------------------------------
  467.  
  468. function ezSetY($y){
  469.   // used to change the vertical position of the writing point.
  470.   $this->y = $y;
  471.   if ( $this->y < $this->ez['bottomMargin']){
  472.     // then make a new page
  473.     $this->ezNewPage();
  474.   }
  475. }
  476.  
  477. // ------------------------------------------------------------------------------
  478.  
  479. function ezSetDy($dy,$mod=''){
  480.   // used to change the vertical position of the writing point.
  481.   // changes up by a positive increment, so enter a negative number to go
  482.   // down the page
  483.   // if $mod is set to 'makeSpace' and a new page is forced, then the pointed will be moved 
  484.   // down on the new page, this will allow space to be reserved for graphics etc.
  485.   $this->y += $dy;
  486.   if ( $this->y < $this->ez['bottomMargin']){
  487.     // then make a new page
  488.     $this->ezNewPage();
  489.     if ($mod=='makeSpace'){
  490.       $this->y += $dy;
  491.     }
  492.   }
  493. }
  494.  
  495. // ------------------------------------------------------------------------------
  496.  
  497. function ezPrvtTableDrawLines($pos,$gap,$x0,$x1,$y0,$y1,$y2,$col,$inner,$outer,$opt=1){
  498.   $x0=1000;
  499.   $x1=0;
  500.   $this->setStrokeColor($col[0],$col[1],$col[2]);
  501.   $cnt=0;
  502.   $n = count($pos);
  503.   foreach($pos as $x){
  504.     $cnt++;
  505.     if ($cnt==1 || $cnt==$n){
  506.       $this->setLineStyle($outer);
  507.     } else {
  508.       $this->setLineStyle($inner);
  509.     }
  510.     $this->line($x-$gap/2,$y0,$x-$gap/2,$y2);
  511.     if ($x>$x1){ $x1=$x; };
  512.     if ($x<$x0){ $x0=$x; };
  513.   }
  514.   $this->setLineStyle($outer);
  515.   $this->line($x0-$gap/2-$outer/2,$y0,$x1-$gap/2+$outer/2,$y0);
  516.   // only do the second line if it is different to the first, AND each row does not have
  517.   // a line on it.
  518.   if ($y0!=$y1 && $opt<2){
  519.     $this->line($x0-$gap/2,$y1,$x1-$gap/2,$y1);
  520.   }
  521.   $this->line($x0-$gap/2-$outer/2,$y2,$x1-$gap/2+$outer/2,$y2);
  522. }
  523.  
  524. // ------------------------------------------------------------------------------
  525.  
  526. function ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$gap,$size,&$y,$optionsAll=array()){
  527.   // uses ezText to add the text, and returns the height taken by the largest heading
  528.   // this page will move the headings to a new page if they will not fit completely on this one
  529.   // transaction support will be used to implement this
  530.  
  531.   if (isset($optionsAll['cols'])){
  532.     $options = $optionsAll['cols'];
  533.   } else {
  534.     $options = array();
  535.   }
  536.   
  537.   $mx=0;
  538.   $startPage = $this->ezPageCount;
  539.   $secondGo=0;
  540.  
  541.   // $y is the position at which the top of the table should start, so the base
  542.   // of the first text, is $y-$height-$gap-$decender, but ezText starts by dropping $height
  543.   
  544.   // the return from this function is the total cell height, including gaps, and $y is adjusted
  545.   // to be the postion of the bottom line
  546.   
  547.   // begin the transaction
  548.   $this->transaction('start');
  549.   $ok=0;
  550. //  $y-=$gap-$decender;
  551.   $y-=$gap;
  552.   while ($ok==0){
  553.     foreach($cols as $colName=>$colHeading){
  554.       $this->ezSetY($y);
  555.       if (isset($options[$colName]) && isset($options[$colName]['justification'])){
  556.         $justification = $options[$colName]['justification'];
  557.       } else {
  558.         $justification = 'left';
  559.       }
  560.       $this->ezText($colHeading,$size,array('aleft'=> $pos[$colName],'aright'=>($maxWidth[$colName]+$pos[$colName]),'justification'=>$justification));
  561.       $dy = $y-$this->y;
  562.       if ($dy>$mx){
  563.         $mx=$dy;
  564.       }
  565.     }
  566.     $y = $y - $mx - $gap + $decender;
  567. //    $y -= $mx-$gap+$decender;
  568.     
  569.     // now, if this has moved to a new page, then abort the transaction, move to a new page, and put it there
  570.     // do not check on the second time around, to avoid an infinite loop
  571.     if ($this->ezPageCount != $startPage && $secondGo==0){
  572.       $this->transaction('rewind');
  573.       $this->ezNewPage();
  574.       $y = $this->y - $gap-$decender;
  575.       $ok=0;
  576.       $secondGo=1;
  577. //      $y = $store_y;
  578.       $mx=0;
  579.  
  580.     } else {
  581.       $this->transaction('commit');
  582.       $ok=1;
  583.     }
  584.   }
  585.  
  586.   return $mx+$gap*2-$decender;
  587. }
  588.  
  589. // ------------------------------------------------------------------------------
  590.  
  591. function ezPrvtGetTextWidth($size,$text){
  592.   // will calculate the maximum width, taking into account that the text may be broken
  593.   // by line breaks.
  594.   $mx=0;
  595.   $lines = explode("\n",$text);
  596.   foreach ($lines as $line){
  597.     $w = $this->getTextWidth($size,$line);
  598.     if ($w>$mx){
  599.       $mx=$w;
  600.     }
  601.   }
  602.   return $mx;
  603. }
  604.  
  605. // ------------------------------------------------------------------------------
  606.  
  607. function ezTable(&$data,$cols='',$title='',$options=''){
  608.   // add a table of information to the pdf document
  609.   // $data is a two dimensional array
  610.   // $cols (optional) is an associative array, the keys are the names of the columns from $data
  611.   // to be presented (and in that order), the values are the titles to be given to the columns
  612.   // $title (optional) is the title to be put on the top of the table
  613.   //
  614.   // $options is an associative array which can contain:
  615.   // 'showLines'=> 0,1,2, default is 1 (show outside and top lines only), 2=> lines on each row
  616.   // 'showHeadings' => 0 or 1
  617.   // 'shaded'=> 0,1,2,3 default is 1 (1->alternate lines are shaded, 0->no shading, 2-> both shaded, second uses shadeCol2)
  618.   // 'shadeCol' => (r,g,b) array, defining the colour of the shading, default is (0.8,0.8,0.8)
  619.   // 'shadeCol2' => (r,g,b) array, defining the colour of the shading of the other blocks, default is (0.7,0.7,0.7)
  620.   // 'fontSize' => 10
  621.   // 'textCol' => (r,g,b) array, text colour
  622.   // 'titleFontSize' => 12
  623.   // 'rowGap' => 2 , the space added at the top and bottom of each row, between the text and the lines
  624.   // 'colGap' => 5 , the space on the left and right sides of each cell
  625.   // 'lineCol' => (r,g,b) array, defining the colour of the lines, default, black.
  626.   // 'xPos' => 'left','right','center','centre',or coordinate, reference coordinate in the x-direction
  627.   // 'xOrientation' => 'left','right','center','centre', position of the table w.r.t 'xPos' 
  628.   // 'width'=> <number> which will specify the width of the table, if it turns out to not be this
  629.   //    wide, then it will stretch the table to fit, if it is wider then each cell will be made
  630.   //    proportionalty smaller, and the content may have to wrap.
  631.   // 'maxWidth'=> <number> similar to 'width', but will only make table smaller than it wants to be
  632.   // 'options' => array(<colname>=>array('justification'=>'left','width'=>100,'link'=>linkDataName),<colname>=>....)
  633.   //             allow the setting of other paramaters for the individual columns
  634.   // 'minRowSpace'=> the minimum space between the bottom of each row and the bottom margin, in which a new row will be started
  635.   //                  if it is less, then a new page would be started, default=-100
  636.   // 'innerLineThickness'=>1
  637.   // 'outerLineThickness'=>1
  638.   // 'splitRows'=>0, 0 or 1, whether or not to allow the rows to be split across page boundaries
  639.   // 'protectRows'=>number, the number of rows to hold with the heading on page, ie, if there less than this number of
  640.   //                rows on the page, then move the whole lot onto the next page, default=1
  641.   //
  642.   // note that the user will have had to make a font selection already or this will not 
  643.   // produce a valid pdf file.
  644.   
  645.   if (!is_array($data)){
  646.     return;
  647.   }
  648.   
  649.   if (!is_array($cols)){
  650.     // take the columns from the first row of the data set
  651.     reset($data);
  652.     list($k,$v)=each($data);
  653.     if (!is_array($v)){
  654.       return;
  655.     }
  656.     $cols=array();
  657.     foreach($v as $k1=>$v1){
  658.       $cols[$k1]=$k1;
  659.     }
  660.   }
  661.   
  662.   if (!is_array($options)){
  663.     $options=array();
  664.   }
  665.  
  666.   $defaults = array(
  667.     'shaded'=>1,'showLines'=>1,'shadeCol'=>array(0.8,0.8,0.8),'shadeCol2'=>array(0.7,0.7,0.7),'fontSize'=>10,'titleFontSize'=>12
  668.     ,'titleGap'=>5,'lineCol'=>array(0,0,0),'gap'=>5,'xPos'=>'centre','xOrientation'=>'centre'
  669.     ,'showHeadings'=>1,'textCol'=>array(0,0,0),'width'=>0,'maxWidth'=>0,'cols'=>array(),'minRowSpace'=>-100,'rowGap'=>2,'colGap'=>5
  670.     ,'innerLineThickness'=>1,'outerLineThickness'=>1,'splitRows'=>0,'protectRows'=>1
  671.     );
  672.  
  673.   foreach($defaults as $key=>$value){
  674.     if (is_array($value)){
  675.       if (!isset($options[$key]) || !is_array($options[$key])){
  676.         $options[$key]=$value;
  677.       }
  678.     } else {
  679.       if (!isset($options[$key])){
  680.         $options[$key]=$value;
  681.       }
  682.     }
  683.   }
  684.   $options['gap']=2*$options['colGap'];
  685.   
  686.   $middle = ($this->ez['pageWidth']-$this->ez['rightMargin'])/2+($this->ez['leftMargin'])/2;
  687.   // figure out the maximum widths of the text within each column
  688.   $maxWidth=array();
  689.   foreach($cols as $colName=>$colHeading){
  690.     $maxWidth[$colName]=0;
  691.   }
  692.   // find the maximum cell widths based on the data
  693.   foreach($data as $row){
  694.     foreach($cols as $colName=>$colHeading){
  695.       $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$row[$colName])*1.01;
  696.       if ($w > $maxWidth[$colName]){
  697.         $maxWidth[$colName]=$w;
  698.       }
  699.     }
  700.   }
  701.   // and the maximum widths to fit in the headings
  702.   foreach($cols as $colName=>$colTitle){
  703.     $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$colTitle)*1.01;
  704.     if ($w > $maxWidth[$colName]){
  705.       $maxWidth[$colName]=$w;
  706.     }
  707.   }
  708.   
  709.   // calculate the start positions of each of the columns
  710.   $pos=array();
  711.   $x=0;
  712.   $t=$x;
  713.   $adjustmentWidth=0;
  714.   $setWidth=0;
  715.   foreach($maxWidth as $colName => $w){
  716.     $pos[$colName]=$t;
  717.     // if the column width has been specified then set that here, also total the
  718.     // width avaliable for adjustment
  719.     if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['width']) && $options['cols'][$colName]['width']>0){
  720.       $t=$t+$options['cols'][$colName]['width'];
  721.       $maxWidth[$colName] = $options['cols'][$colName]['width']-$options['gap'];
  722.       $setWidth += $options['cols'][$colName]['width'];
  723.     } else {
  724.       $t=$t+$w+$options['gap'];
  725.       $adjustmentWidth += $w;
  726.       $setWidth += $options['gap'];
  727.     }
  728.   }
  729.   $pos['_end_']=$t;
  730.  
  731.   // if maxWidth is specified, and the table is too wide, and the width has not been set,
  732.   // then set the width.
  733.   if ($options['width']==0 && $options['maxWidth'] && ($t-$x)>$options['maxWidth']){
  734.     // then need to make this one smaller
  735.     $options['width']=$options['maxWidth'];
  736.   }
  737.  
  738.   if ($options['width'] && $adjustmentWidth>0 && $setWidth<$options['width']){
  739.     // first find the current widths of the columns involved in this mystery
  740.     $cols0 = array();
  741.     $cols1 = array();
  742.     $xq=0;
  743.     $presentWidth=0;
  744.     $last='';
  745.     foreach($pos as $colName=>$p){
  746.       if (!isset($options['cols'][$last]) || !isset($options['cols'][$last]['width']) || $options['cols'][$last]['width']<=0){
  747.         if (strlen($last)){
  748.           $cols0[$last]=$p-$xq -$options['gap'];
  749.           $presentWidth += ($p-$xq - $options['gap']);
  750.         }
  751.       } else {
  752.         $cols1[$last]=$p-$xq;
  753.       }
  754.       $last=$colName;
  755.       $xq=$p;
  756.     }
  757.     // $cols0 contains the widths of all the columns which are not set
  758.     $neededWidth = $options['width']-$setWidth;
  759.     // if needed width is negative then add it equally to each column, else get more tricky
  760.     if ($presentWidth<$neededWidth){
  761.       foreach($cols0 as $colName=>$w){
  762.         $cols0[$colName]+= ($neededWidth-$presentWidth)/count($cols0);
  763.       }
  764.     } else {
  765.     
  766.       $cnt=0;
  767.       while ($presentWidth>$neededWidth && $cnt<100){
  768.         $cnt++; // insurance policy
  769.         // find the widest columns, and the next to widest width
  770.         $aWidest = array();
  771.         $nWidest=0;
  772.         $widest=0;
  773.         foreach($cols0 as $colName=>$w){
  774.           if ($w>$widest){
  775.             $aWidest=array($colName);
  776.             $nWidest = $widest;
  777.             $widest=$w;
  778.           } else if ($w==$widest){
  779.             $aWidest[]=$colName;
  780.           }
  781.         }
  782.         // then figure out what the width of the widest columns would have to be to take up all the slack
  783.         $newWidestWidth = $widest - ($presentWidth-$neededWidth)/count($aWidest);
  784.         if ($newWidestWidth > $nWidest){
  785.           // then there is space to set them to this
  786.           foreach($aWidest as $colName){
  787.             $cols0[$colName] = $newWidestWidth;
  788.           }
  789.           $presentWidth=$neededWidth;
  790.         } else {
  791.           // there is not space, reduce the size of the widest ones down to the next size down, and we
  792.           // will go round again
  793.           foreach($aWidest as $colName){
  794.             $cols0[$colName] = $nWidest;
  795.           }
  796.           $presentWidth=$presentWidth-($widest-$nWidest)*count($aWidest);
  797.         }
  798.       }
  799.     }
  800.     // $cols0 now contains the new widths of the constrained columns.
  801.     // now need to update the $pos and $maxWidth arrays
  802.     $xq=0;
  803.     foreach($pos as $colName=>$p){
  804.       $pos[$colName]=$xq;
  805.       if (!isset($options['cols'][$colName]) || !isset($options['cols'][$colName]['width']) || $options['cols'][$colName]['width']<=0){
  806.         if (isset($cols0[$colName])){
  807.           $xq += $cols0[$colName] + $options['gap'];
  808.           $maxWidth[$colName]=$cols0[$colName];
  809.         }
  810.       } else {
  811.         if (isset($cols1[$colName])){
  812.           $xq += $cols1[$colName];
  813.         }
  814.       }
  815.     }
  816.  
  817.     $t=$x+$options['width'];
  818.     $pos['_end_']=$t;
  819.   }
  820.  
  821.   // now adjust the table to the correct location across the page
  822.   switch ($options['xPos']){
  823.     case 'left':
  824.       $xref = $this->ez['leftMargin'];
  825.       break;
  826.     case 'right':
  827.       $xref = $this->ez['pageWidth'] - $this->ez['rightMargin'];
  828.       break;
  829.     case 'centre':
  830.     case 'center':
  831.       $xref = $middle;
  832.       break;
  833.     default:
  834.       $xref = $options['xPos'];
  835.       break;
  836.   }
  837.   switch ($options['xOrientation']){
  838.     case 'left':
  839.       $dx = $xref-$t;
  840.       break;
  841.     case 'right':
  842.       $dx = $xref;
  843.       break;
  844.     case 'centre':
  845.     case 'center':
  846.       $dx = $xref-$t/2;
  847.       break;
  848.   }
  849.  
  850.  
  851.   foreach($pos as $k=>$v){
  852.     $pos[$k]=$v+$dx;
  853.   }
  854.   $x0=$x+$dx;
  855.   $x1=$t+$dx;
  856.  
  857.   $baseLeftMargin = $this->ez['leftMargin'];
  858.   $basePos = $pos;
  859.   $baseX0 = $x0;
  860.   $baseX1 = $x1;
  861.   
  862.   // ok, just about ready to make me a table
  863.   $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2]);
  864.   $this->setStrokeColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2]);
  865.  
  866.   $middle = ($x1+$x0)/2;
  867.  
  868.   // start a transaction which will be used to regress the table, if there are not enough rows protected
  869.   if ($options['protectRows']>0){
  870.     $this->transaction('start');
  871.     $movedOnce=0;
  872.   }
  873.   $abortTable = 1;
  874.   while ($abortTable){
  875.   $abortTable=0;
  876.  
  877.   $dm = $this->ez['leftMargin']-$baseLeftMargin;
  878.   foreach($basePos as $k=>$v){
  879.     $pos[$k]=$v+$dm;
  880.   }
  881.   $x0=$baseX0+$dm;
  882.   $x1=$baseX1+$dm;
  883.   $middle = ($x1+$x0)/2;
  884.  
  885.  
  886.   // if the title is set, then do that
  887.   if (strlen($title)){
  888.     $w = $this->getTextWidth($options['titleFontSize'],$title);
  889.     $this->y -= $this->getFontHeight($options['titleFontSize']);
  890.     if ($this->y < $this->ez['bottomMargin']){
  891.       $this->ezNewPage();
  892.         // margins may have changed on the newpage
  893.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  894.         foreach($basePos as $k=>$v){
  895.           $pos[$k]=$v+$dm;
  896.         }
  897.         $x0=$baseX0+$dm;
  898.         $x1=$baseX1+$dm;
  899.         $middle = ($x1+$x0)/2;
  900.       $this->y -= $this->getFontHeight($options['titleFontSize']);
  901.     }
  902.     $this->addText($middle-$w/2,$this->y,$options['titleFontSize'],$title);
  903.     $this->y -= $options['titleGap'];
  904.   }
  905.  
  906.         // margins may have changed on the newpage
  907.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  908.         foreach($basePos as $k=>$v){
  909.           $pos[$k]=$v+$dm;
  910.         }
  911.         $x0=$baseX0+$dm;
  912.         $x1=$baseX1+$dm;
  913.   
  914.   $y=$this->y; // to simplify the code a bit
  915.   
  916.   // make the table
  917.   $height = $this->getFontHeight($options['fontSize']);
  918.   $decender = $this->getFontDecender($options['fontSize']);
  919.  
  920.   
  921.   
  922.   $y0=$y+$decender;
  923.   $dy=0;
  924.   if ($options['showHeadings']){
  925.     // this function will move the start of the table to a new page if it does not fit on this one
  926.     $headingHeight = $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  927.     $y0 = $y+$headingHeight;
  928.     $y1 = $y;
  929.  
  930.  
  931.     $dm = $this->ez['leftMargin']-$baseLeftMargin;
  932.     foreach($basePos as $k=>$v){
  933.       $pos[$k]=$v+$dm;
  934.     }
  935.     $x0=$baseX0+$dm;
  936.     $x1=$baseX1+$dm;
  937.  
  938.   } else {
  939.     $y1 = $y0;
  940.   }
  941.   $firstLine=1;
  942.  
  943.   
  944.   // open an object here so that the text can be put in over the shading
  945.   if ($options['shaded']){
  946.     $this->saveState();
  947.     $textObjectId = $this->openObject();
  948.     $this->closeObject();
  949.     $this->addObject($textObjectId);
  950.     $this->reopenObject($textObjectId);
  951.   }
  952.   
  953.   $cnt=0;
  954.   $newPage=0;
  955.   foreach($data as $row){
  956.     $cnt++;
  957.     // the transaction support will be used to prevent rows being split
  958.     if ($options['splitRows']==0){
  959.       $pageStart = $this->ezPageCount;
  960.       if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  961.         $columnStart = $this->ez['columns']['colNum'];
  962.       }
  963.       $this->transaction('start');
  964.       $row_orig = $row;
  965.       $y_orig = $y;
  966.       $y0_orig = $y0;
  967.       $y1_orig = $y1;
  968.     }
  969.     $ok=0;
  970.     $secondTurn=0;
  971.     while(!$abortTable && $ok == 0){
  972.  
  973.     $mx=0;
  974.     $newRow=1;
  975.     while(!$abortTable && ($newPage || $newRow)){
  976.       
  977.       $y-=$height;
  978.       if ($newPage || $y<$this->ez['bottomMargin'] || (isset($options['minRowSpace']) && $y<($this->ez['bottomMargin']+$options['minRowSpace'])) ){
  979.         // check that enough rows are with the heading
  980.         if ($options['protectRows']>0 && $movedOnce==0 && $cnt<=$options['protectRows']){
  981.           // then we need to move the whole table onto the next page
  982.           $movedOnce = 1;
  983.           $abortTable = 1;
  984.         }
  985.       
  986.         $y2=$y-$mx+2*$height+$decender-$newRow*$height;
  987.         if ($options['showLines']){
  988.           if (!$options['showHeadings']){
  989.             $y0=$y1;
  990.           }
  991.           $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  992.         }
  993.         if ($options['shaded']){
  994.           $this->closeObject();
  995.           $this->restoreState();
  996.         }
  997.         $this->ezNewPage();
  998.         // and the margins may have changed, this is due to the possibility of the columns being turned on
  999.         // as the columns are managed by manipulating the margins
  1000.  
  1001.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  1002.         foreach($basePos as $k=>$v){
  1003.           $pos[$k]=$v+$dm;
  1004.         }
  1005. //        $x0=$x0+$dm;
  1006. //        $x1=$x1+$dm;
  1007.         $x0=$baseX0+$dm;
  1008.         $x1=$baseX1+$dm;
  1009.   
  1010.         if ($options['shaded']){
  1011.           $this->saveState();
  1012.           $textObjectId = $this->openObject();
  1013.           $this->closeObject();
  1014.           $this->addObject($textObjectId);
  1015.           $this->reopenObject($textObjectId);
  1016.         }
  1017.         $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
  1018.         $y = $this->ez['pageHeight']-$this->ez['topMargin'];
  1019.         $y0=$y+$decender;
  1020.         $mx=0;
  1021.         if ($options['showHeadings']){
  1022.           $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  1023.           $y1=$y;
  1024.         } else {
  1025.           $y1=$y0;
  1026.         }
  1027.         $firstLine=1;
  1028.         $y -= $height;
  1029.       }
  1030.       $newRow=0;
  1031.       // write the actual data
  1032.       // if these cells need to be split over a page, then $newPage will be set, and the remaining
  1033.       // text will be placed in $leftOvers
  1034.       $newPage=0;
  1035.       $leftOvers=array();
  1036.  
  1037.       foreach($cols as $colName=>$colTitle){
  1038.         $this->ezSetY($y+$height);
  1039.         $colNewPage=0;
  1040.         if (isset($row[$colName])){
  1041.           if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['link']) && strlen($options['cols'][$colName]['link'])){
  1042.             
  1043.             $lines = explode("\n",$row[$colName]);
  1044.             if (isset($row[$options['cols'][$colName]['link']]) && strlen($row[$options['cols'][$colName]['link']])){
  1045.               foreach($lines as $k=>$v){
  1046.                 $lines[$k]='<c:alink:'.$row[$options['cols'][$colName]['link']].'>'.$v.'</c:alink>';
  1047.               }
  1048.             }
  1049.           } else {
  1050.             $lines = explode("\n",$row[$colName]);
  1051.           }
  1052.         } else {
  1053.           $lines = array();
  1054.         }
  1055.         $this->y -= $options['rowGap'];
  1056.         foreach ($lines as $line){
  1057.           $line = $this->ezProcessText($line);
  1058.           $start=1;
  1059.  
  1060.           while (strlen($line) || $start){
  1061.             $start=0;
  1062.             if (!$colNewPage){
  1063.               $this->y=$this->y-$height;
  1064.             }
  1065.             if ($this->y < $this->ez['bottomMargin']){
  1066.   //            $this->ezNewPage();
  1067.               $newPage=1;  // whether a new page is required for any of the columns
  1068.               $colNewPage=1; // whether a new page is required for this column
  1069.             }
  1070.             if ($colNewPage){
  1071.               if (isset($leftOvers[$colName])){
  1072.                 $leftOvers[$colName].="\n".$line;
  1073.               } else {
  1074.                 $leftOvers[$colName] = $line;
  1075.               }
  1076.               $line='';
  1077.             } else {
  1078.               if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['justification']) ){
  1079.                 $just = $options['cols'][$colName]['justification'];
  1080.               } else {
  1081.                 $just='left';
  1082.               }
  1083.  
  1084.               $line=$this->addTextWrap($pos[$colName],$this->y,$maxWidth[$colName],$options['fontSize'],$line,$just);
  1085.             }
  1086.           }
  1087.         }
  1088.   
  1089.         $dy=$y+$height-$this->y+$options['rowGap'];
  1090.         if ($dy-$height*$newPage>$mx){
  1091.           $mx=$dy-$height*$newPage;
  1092.         }
  1093.       }
  1094.       // set $row to $leftOvers so that they will be processed onto the new page
  1095.       $row = $leftOvers;
  1096.       // now add the shading underneath
  1097.       if ($options['shaded'] && $cnt%2==0){
  1098.         $this->closeObject();
  1099.         $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
  1100.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1101.         $this->reopenObject($textObjectId);
  1102.       }
  1103.  
  1104.       if ($options['shaded']==2 && $cnt%2==1){
  1105.         $this->closeObject();
  1106.         $this->setColor($options['shadeCol2'][0],$options['shadeCol2'][1],$options['shadeCol2'][2],1);
  1107.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1108.         $this->reopenObject($textObjectId);
  1109.       }
  1110.  
  1111.       if ($options['showLines']>1){
  1112.         // then draw a line on the top of each block
  1113. //        $this->closeObject();
  1114.         $this->saveState();
  1115.         $this->setStrokeColor($options['lineCol'][0],$options['lineCol'][1],$options['lineCol'][2],1);
  1116. //        $this->line($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1117.         if ($firstLine){
  1118.           $this->setLineStyle($options['outerLineThickness']);
  1119.           $firstLine=0;
  1120.         } else {
  1121.           $this->setLineStyle($options['innerLineThickness']);
  1122.         }
  1123.         $this->line($x0-$options['gap']/2,$y+$decender+$height,$x1-$options['gap']/2,$y+$decender+$height);
  1124.         $this->restoreState();
  1125. //        $this->reopenObject($textObjectId);
  1126.       }
  1127.     } // end of while 
  1128.     $y=$y-$mx+$height;
  1129.     
  1130.     // checking row split over pages
  1131.     if ($options['splitRows']==0){
  1132.       if ( ( ($this->ezPageCount != $pageStart) || (isset($this->ez['columns']) && $this->ez['columns']['on']==1 && $columnStart != $this->ez['columns']['colNum'] ))  && $secondTurn==0){
  1133.         // then we need to go back and try that again !
  1134.         $newPage=1;
  1135.         $secondTurn=1;
  1136.         $this->transaction('rewind');
  1137.         $row = $row_orig;
  1138.         $y = $y_orig;
  1139.         $y0 = $y0_orig;
  1140.         $y1 = $y1_orig;
  1141.         $ok=0;
  1142.  
  1143.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  1144.         foreach($basePos as $k=>$v){
  1145.           $pos[$k]=$v+$dm;
  1146.         }
  1147.         $x0=$baseX0+$dm;
  1148.         $x1=$baseX1+$dm;
  1149.  
  1150.       } else {
  1151.         $this->transaction('commit');
  1152.         $ok=1;
  1153.       }
  1154.     } else {
  1155.       $ok=1;  // don't go round the loop if splitting rows is allowed
  1156.     }
  1157.     
  1158.     }  // end of while to check for row splitting
  1159.     if ($abortTable){
  1160.       if ($ok==0){
  1161.         $this->transaction('abort');
  1162.       }
  1163.       // only the outer transaction should be operational
  1164.       $this->transaction('rewind');
  1165.       $this->ezNewPage();
  1166.       break;
  1167.     }
  1168.     
  1169.   } // end of foreach ($data as $row)
  1170.   
  1171.   } // end of while ($abortTable)
  1172.  
  1173.   // table has been put on the page, the rows guarded as required, commit.
  1174.   $this->transaction('commit');
  1175.  
  1176.   $y2=$y+$decender;
  1177.   if ($options['showLines']){
  1178.     if (!$options['showHeadings']){
  1179.       $y0=$y1;
  1180.     }
  1181.     $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  1182.   }
  1183.  
  1184.   // close the object for drawing the text on top
  1185.   if ($options['shaded']){
  1186.     $this->closeObject();
  1187.     $this->restoreState();
  1188.   }
  1189.   
  1190.   $this->y=$y;
  1191.   return $y;
  1192. }
  1193.  
  1194. // ------------------------------------------------------------------------------
  1195. function ezProcessText($text){
  1196.   // this function will intially be used to implement underlining support, but could be used for a range of other
  1197.   // purposes
  1198.   $search = array('<u>','<U>','</u>','</U>');
  1199.   $replace = array('<c:uline>','<c:uline>','</c:uline>','</c:uline>');
  1200.   return str_replace($search,$replace,$text);
  1201. }
  1202.  
  1203. // ------------------------------------------------------------------------------
  1204.  
  1205. function ezText($text,$size=0,$options=array(),$test=0){
  1206.   // this will add a string of text to the document, starting at the current drawing
  1207.   // position.
  1208.   // it will wrap to keep within the margins, including optional offsets from the left
  1209.   // and the right, if $size is not specified, then it will be the last one used, or
  1210.   // the default value (12 I think).
  1211.   // the text will go to the start of the next line when a return code "\n" is found.
  1212.   // possible options are:
  1213.   // 'left'=> number, gap to leave from the left margin
  1214.   // 'right'=> number, gap to leave from the right margin
  1215.   // 'aleft'=> number, absolute left position (overrides 'left')
  1216.   // 'aright'=> number, absolute right position (overrides 'right')
  1217.   // 'justification' => 'left','right','center','centre','full'
  1218.  
  1219.   // only set one of the next two items (leading overrides spacing)
  1220.   // 'leading' => number, defines the total height taken by the line, independent of the font height.
  1221.   // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
  1222.   
  1223.   // if $test is set then this should just check if the text is going to flow onto a new page or not, returning true or false
  1224.   
  1225.   // apply the filtering which will make the underlining function.
  1226.   $text = $this->ezProcessText($text);
  1227.   
  1228.   $newPage=false;
  1229.   $store_y = $this->y;
  1230.   
  1231.   if (is_array($options) && isset($options['aleft'])){
  1232.     $left=$options['aleft'];
  1233.   } else {
  1234.     $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1235.   }
  1236.   if (is_array($options) && isset($options['aright'])){
  1237.     $right=$options['aright'];
  1238.   } else {
  1239.     $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1240.   }
  1241.   if ($size<=0){
  1242.     $size = $this->ez['fontSize'];
  1243.   } else {
  1244.     $this->ez['fontSize']=$size;
  1245.   }
  1246.   
  1247.   if (is_array($options) && isset($options['justification'])){
  1248.     $just = $options['justification'];
  1249.   } else {
  1250.     $just = 'left';
  1251.   }
  1252.   
  1253.   // modifications to give leading and spacing based on those given by Craig Heydenburg 1/1/02
  1254.   if (is_array($options) && isset($options['leading'])) { ## use leading instead of spacing
  1255.     $height = $options['leading'];
  1256.     } else if (is_array($options) && isset($options['spacing'])) {
  1257.     $height = $this->getFontHeight($size) * $options['spacing'];
  1258.     } else {
  1259.         $height = $this->getFontHeight($size);
  1260.     }
  1261.  
  1262.   
  1263.   $lines = explode("\n",$text);
  1264.   foreach ($lines as $line){
  1265.     $start=1;
  1266.     while (strlen($line) || $start){
  1267.       $start=0;
  1268.       $this->y=$this->y-$height;
  1269.       if ($this->y < $this->ez['bottomMargin']){
  1270.         if ($test){
  1271.           $newPage=true;
  1272.         } else {
  1273.           $this->ezNewPage();
  1274.           // and then re-calc the left and right, in case they have changed due to columns
  1275.         }
  1276.       }
  1277.       if (is_array($options) && isset($options['aleft'])){
  1278.         $left=$options['aleft'];
  1279.       } else {
  1280.         $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1281.       }
  1282.       if (is_array($options) && isset($options['aright'])){
  1283.         $right=$options['aright'];
  1284.       } else {
  1285.         $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1286.       }
  1287.       $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
  1288.     }
  1289.   }
  1290.  
  1291.   if ($test){
  1292.     $this->y=$store_y;
  1293.     return $newPage;
  1294.   } else {
  1295.     return $this->y;
  1296.   }
  1297. }
  1298.  
  1299. // ------------------------------------------------------------------------------
  1300.  
  1301. function ezImage($image,$pad = 5,$width = 0,$resize = 'full',$just = 'center',$border = ''){
  1302.     //beta ezimage function
  1303.     if (stristr($image,'://'))//copy to temp file
  1304.     {
  1305.         $fp = @fopen($image,"rb");
  1306.         while(!feof($fp))
  1307.            {
  1308.                   $cont.= fread($fp,1024);
  1309.            }
  1310.            fclose($fp);
  1311.         $image = tempnam ("/tmp", "php-pdf");
  1312.         $fp2 = @fopen($image,"w");
  1313.            fwrite($fp2,$cont);
  1314.           fclose($fp2);
  1315.         $temp = true;
  1316.     }
  1317.  
  1318.     if (!(file_exists($image))) return false; //return immediately if image file does not exist
  1319.     $imageInfo = getimagesize($image);
  1320.     switch ($imageInfo[2]){
  1321.         case 2:
  1322.             $type = "jpeg";
  1323.             break;
  1324.         case 3:
  1325.             $type = "png";
  1326.             break;
  1327.         default:
  1328.             return false; //return if file is not jpg or png
  1329.     }
  1330.     if ($width == 0) $width = $imageInfo[0]; //set width
  1331.     $ratio = $imageInfo[0]/$imageInfo[1];
  1332.  
  1333.     //get maximum width of image
  1334.     if (isset($this->ez['columns']) && $this->ez['columns']['on'] == 1)
  1335.     {
  1336.         $bigwidth = $this->ez['columns']['width'] - ($pad * 2);
  1337.     }
  1338.     else
  1339.     {
  1340.         $bigwidth = $this->ez['pageWidth'] - ($pad * 2);
  1341.     }
  1342.     //fix width if larger than maximum or if $resize=full
  1343.     if ($resize == 'full' || $resize == 'width' || $width > $bigwidth)
  1344.     {
  1345.         $width = $bigwidth;
  1346.  
  1347.     }
  1348.  
  1349.     $height = ($width/$ratio); //set height
  1350.  
  1351.     //fix size if runs off page
  1352.     if ($height > ($this->y - $this->ez['bottomMargin'] - ($pad * 2)))
  1353.     {
  1354.         if ($resize != 'full')
  1355.         {
  1356.             $this->ezNewPage();
  1357.         }
  1358.         else
  1359.         {
  1360.             $height = ($this->y - $this->ez['bottomMargin'] - ($pad * 2)); //shrink height
  1361.             $width = ($height*$ratio); //fix width
  1362.         }
  1363.     }
  1364.  
  1365.     //fix x-offset if image smaller than bigwidth
  1366.     if ($width < $bigwidth)
  1367.     {
  1368.         //center if justification=center
  1369.         if ($just == 'center')
  1370.         {
  1371.             $offset = ($bigwidth - $width) / 2;
  1372.         }
  1373.         //move to right if justification=right
  1374.         if ($just == 'right')
  1375.         {
  1376.             $offset = ($bigwidth - $width);
  1377.         }
  1378.         //leave at left if justification=left
  1379.         if ($just == 'left')
  1380.         {
  1381.             $offset = 0;
  1382.         }
  1383.     }
  1384.  
  1385.  
  1386.     //call appropriate function
  1387.     if ($type == "jpeg"){
  1388.         $this->addJpegFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1389.     }
  1390.  
  1391.     if ($type == "png"){
  1392.         $this->addPngFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1393.     }
  1394.     //draw border
  1395.     if ($border != '')
  1396.     {
  1397.     if (!(isset($border['color'])))
  1398.     {
  1399.         $border['color']['red'] = .5;
  1400.         $border['color']['blue'] = .5;
  1401.         $border['color']['green'] = .5;
  1402.     }
  1403.     if (!(isset($border['width']))) $border['width'] = 1;
  1404.     if (!(isset($border['cap']))) $border['cap'] = 'round';
  1405.     if (!(isset($border['join']))) $border['join'] = 'round';
  1406.     
  1407.  
  1408.     $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
  1409.     $this->setLineStyle($border['width'],$border['cap'],$border['join']);
  1410.     $this->rectangle($this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width,$height);
  1411.  
  1412.     }
  1413.     // move y below image
  1414.     $this->y = $this->y - $pad - $height;
  1415.     //remove tempfile for remote images
  1416.     if ($temp == true) unlink($image);
  1417.  
  1418. }
  1419. // ------------------------------------------------------------------------------
  1420.  
  1421. // note that templating code is still considered developmental - have not really figured
  1422. // out a good way of doing this yet.
  1423. function loadTemplate($templateFile){
  1424.   // this function will load the requested template ($file includes full or relative pathname)
  1425.   // the code for the template will be modified to make it name safe, and then stored in 
  1426.   // an array for later use
  1427.   // The id of the template will be returned for the user to operate on it later
  1428.   if (!file_exists($templateFile)){
  1429.     return -1;
  1430.   }
  1431.  
  1432.   $code = implode('',file($templateFile));
  1433.   if (!strlen($code)){
  1434.     return;
  1435.   }
  1436.  
  1437.   $code = trim($code);
  1438.   if (substr($code,0,5)=='<?php'){
  1439.     $code = substr($code,5);
  1440.   }
  1441.   if (substr($code,-2)=='?>'){
  1442.     $code = substr($code,0,strlen($code)-2);
  1443.   }
  1444.   if (isset($this->ez['numTemplates'])){
  1445.     $newNum = $this->ez['numTemplates'];
  1446.     $this->ez['numTemplates']++;
  1447.   } else {
  1448.     $newNum=0;
  1449.     $this->ez['numTemplates']=1;
  1450.     $this->ez['templates']=array();
  1451.   }
  1452.  
  1453.   $this->ez['templates'][$newNum]['code']=$code;
  1454.  
  1455.   return $newNum;
  1456. }
  1457.  
  1458. // ------------------------------------------------------------------------------
  1459.  
  1460. function execTemplate($id,$data=array(),$options=array()){
  1461.   // execute the given template on the current document.
  1462.   if (!isset($this->ez['templates'][$id])){
  1463.     return;
  1464.   }
  1465.   eval($this->ez['templates'][$id]['code']);
  1466. }
  1467.  
  1468. // ------------------------------------------------------------------------------
  1469. function ilink($info){
  1470.   $this->alink($info,1);
  1471. }
  1472.  
  1473. function alink($info,$internal=0){
  1474.   // a callback function to support the formation of clickable links within the document
  1475.   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1476.   switch($info['status']){
  1477.     case 'start':
  1478.     case 'sol':
  1479.       // the beginning of the link
  1480.       // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
  1481.       if (!isset($this->ez['links'])){
  1482.         $this->ez['links']=array();
  1483.       }
  1484.       $i = $info['nCallback'];
  1485.       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
  1486.       if ($internal==0){
  1487.         $this->saveState();
  1488.         $this->setColor(0,0,1);
  1489.         $this->setStrokeColor(0,0,1);
  1490.         $thick = $info['height']*$lineFactor;
  1491.         $this->setLineStyle($thick);
  1492.       }
  1493.       break;
  1494.     case 'end':
  1495.     case 'eol':
  1496.       // the end of the link
  1497.       // assume that it is the most recent opening which has closed
  1498.       $i = $info['nCallback'];
  1499.       $start = $this->ez['links'][$i];
  1500.       // add underlining
  1501.       if ($internal){
  1502.         $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1503.       } else {
  1504.         $a = deg2rad((float)$start['angle']-90.0);
  1505.         $drop = $start['height']*$lineFactor*1.5;
  1506.         $dropx = cos($a)*$drop;
  1507.         $dropy = -sin($a)*$drop;
  1508.         $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1509.         $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1510.         $this->restoreState();
  1511.       }
  1512.       break;
  1513.   }
  1514. }
  1515.  
  1516. // ------------------------------------------------------------------------------
  1517.  
  1518. function uline($info){
  1519.   // a callback function to support underlining
  1520.   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1521.   switch($info['status']){
  1522.     case 'start':
  1523.     case 'sol':
  1524.     
  1525.       // the beginning of the underline zone
  1526.       if (!isset($this->ez['links'])){
  1527.         $this->ez['links']=array();
  1528.       }
  1529.       $i = $info['nCallback'];
  1530.       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
  1531.       $this->saveState();
  1532.       $thick = $info['height']*$lineFactor;
  1533.       $this->setLineStyle($thick);
  1534.       break;
  1535.     case 'end':
  1536.     case 'eol':
  1537.       // the end of the link
  1538.       // assume that it is the most recent opening which has closed
  1539.       $i = $info['nCallback'];
  1540.       $start = $this->ez['links'][$i];
  1541.       // add underlining
  1542.       $a = deg2rad((float)$start['angle']-90.0);
  1543.       $drop = $start['height']*$lineFactor*1.5;
  1544.       $dropx = cos($a)*$drop;
  1545.       $dropy = -sin($a)*$drop;
  1546.       $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1547.       $this->restoreState();
  1548.       break;
  1549.   }
  1550. }
  1551.  
  1552. // ------------------------------------------------------------------------------
  1553.  
  1554. }
  1555. ?>